#include "scrollbutton.h"
#include <QPainter>
#include <QPixmap>
#include <QMatrix>

#include "../terminalglobals.h"

using namespace ExecTerminalImplNS;

ScrollButton::ScrollButton(QWidget * parent): QPushButton(parent),
theOrientation(UP_ORIENTATION)
{

}

Orientation ScrollButton::orientation()const
{
    return theOrientation;
}

void ScrollButton::setOrientation(Orientation ornt)
{
    if (ornt != theOrientation)
    {
        theOrientation = ornt;
        update();
    }
}

void ScrollButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap leftPix;
    QPixmap centerPix;
    QPixmap rightPix;
    QPixmap arrow;
    if (isDown())
    {
        leftPix = pixmapForPath(":Resources/left_bookmarks_mover_left_down.png");
        centerPix = pixmapForPath(":Resources/left_bookmarks_mover_center_down.png");
        rightPix = pixmapForPath(":Resources/left_bookmarks_mover_right_down.png");
        arrow = pixmapForPath(":Resources/left_bookmarks_mover_arrow_down.png");
    }
    else if (underMouse() || !isEnabled())
    {
        leftPix = pixmapForPath(":Resources/left_bookmarks_mover_left_hover.png");
        centerPix = pixmapForPath(":Resources/left_bookmarks_mover_center_hover.png");
        rightPix = pixmapForPath(":Resources/left_bookmarks_mover_right_hover.png");
        arrow = pixmapForPath(":Resources/left_bookmarks_mover_arrow_hover.png");
    }
    else
    {
        leftPix = pixmapForPath(":Resources/left_bookmarks_mover_left.png");
        centerPix = pixmapForPath(":Resources/left_bookmarks_mover_center.png");
        rightPix = pixmapForPath(":Resources/left_bookmarks_mover_right.png");
        arrow = pixmapForPath(":Resources/left_bookmarks_mover_arrow.png");
    }
    painter.drawPixmap(0, 0, leftPix);
    const QRect rectForTile(0 + leftPix.width(), 0, width() - leftPix.width() - rightPix.width(), height());
    painter.drawTiledPixmap(rectForTile, centerPix);
    painter.drawPixmap(rectForTile.x() + rectForTile.width(), 0,  rightPix);
    if (orientation() == DOWN_ORIENTATION)
    {
        QMatrix rotMatrix;
        rotMatrix.translate(width(), height());
        rotMatrix.rotate(180);
        painter.setMatrix(rotMatrix);
    }
    painter.drawPixmap(width() / 2 - arrow.width() / 2, height() / 2 - arrow.height() / 2, arrow);
}



